package com.keyue.service.admin.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.keyue.common.constant.Constant;
import com.keyue.common.constant.ServerResult;
import com.keyue.domain.dto.BaseDO;
import com.keyue.domain.dto.admin.AdminMenuDO;
import com.keyue.domain.vo.admin.AdminMenuQueryVO;
import com.keyue.domain.vo.admin.AdminMenuResult;
import com.keyue.domain.vo.admin.AdminMenuVO;
import com.keyue.mapper.admin.AdminMenuMapper;
import com.keyue.service.admin.AdminMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 角色表(AdminMenu)表服务实现类
 *
 * @author makejava
 * @since 2023-08-30 17:51:48
 */
@Slf4j
@Service
public class AdminMenuServiceImpl extends ServiceImpl<AdminMenuMapper, AdminMenuDO> implements AdminMenuService {


    /**
     * 通过ID查询单条数据
     * <p>
     * adminMenuQueryVO 主键
     *
     * @return 实例对象
     */
    @Override
    public ServerResult queryById(AdminMenuQueryVO adminMenuQueryVO) {
        if (Objects.isNull(adminMenuQueryVO.getId())) {
            return ServerResult.fail("id 不能为空");
        }
        AdminMenuDO AdminMenuDo = getById(adminMenuQueryVO.getId());
        if (Objects.isNull(AdminMenuDo)) {
            return ServerResult.success(null);
        }
        return ServerResult.success(BeanUtil.copyProperties(AdminMenuDo, AdminMenuVO.class));
    }

    /**
     * 分页查询
     *
     * @param adminMenuQueryVO 筛选条件
     * @return 查询结果
     */
    @Override
    public ServerResult list(AdminMenuQueryVO adminMenuQueryVO) {

        return null;
    }

    @Override
    public ServerResult all(AdminMenuQueryVO adminMenuQueryVO) {
        List<AdminMenuDO> menuDOList = list(new LambdaQueryWrapper<AdminMenuDO>().eq(BaseDO::getEnableFlag, Constant.ENABLE_TEN));

        if (CollectionUtils.isEmpty(menuDOList)) {
            return ServerResult.success();
        }

        return ServerResult.success(buildTree(menuDOList));
    }

    /**
     * @param menuDOList
     * @return
     */
    private List<AdminMenuResult> buildTree(List<AdminMenuDO> menuDOList) {
        List<AdminMenuResult> menuDtos = menuDOList.stream().map(x -> {
            AdminMenuResult result = new AdminMenuResult();
            BeanUtils.copyProperties(x, result);
            return result;
        }).collect(Collectors.toList());

        List<AdminMenuResult> trees = new ArrayList<>();
        Set<Long> ids = new HashSet<>();
        for (AdminMenuResult menuDTO : menuDtos) {
            if (Objects.nonNull(menuDTO.getPid())) {
                trees.add(menuDTO);
            }
            for (AdminMenuResult it : menuDtos) {
                if (Objects.nonNull(it.getPid()) && it.getPid().equals(menuDTO.getId())) {
                    if (menuDTO.getChildren() == null) {
                        menuDTO.setChildren(new ArrayList<>());
                    }
                    menuDTO.getChildren().add(it);
                    ids.add(it.getId());
                }
            }
        }
        if (trees.size() == 0) {
            trees = menuDtos.stream().filter(s -> !ids.contains(s.getId())).collect(Collectors.toList());
        }
        return trees;
    }

    /**
     * 新增数据
     *
     * @param adminMenuVO 实例对象
     * @return 实例对象
     */
    @Override
    public ServerResult add(AdminMenuVO adminMenuVO) {
        //todo 校验
        if (Objects.nonNull(adminMenuVO.getId())) {
            adminMenuVO.setId(null);
        }
        AdminMenuDO adminMenuDo = BeanUtil.copyProperties(adminMenuVO, AdminMenuDO.class);

        save(adminMenuDo);

        return ServerResult.success();
    }

    /**
     * 修改数据
     *
     * @param adminMenuVO 实例对象
     * @return 实例对象
     */
    @Override
    public ServerResult edit(AdminMenuVO adminMenuVO) {
        //todo 校验
        if (Objects.isNull(adminMenuVO.getId())) {
            return ServerResult.fail("id 不能为空");
        }
        AdminMenuDO adminMenuDo = BeanUtil.copyProperties(adminMenuVO, AdminMenuDO.class);

        updateById(adminMenuDo);

        return ServerResult.success();
    }

    /**
     * 通过主键删除数据
     *
     * @param adminMenuQueryVO 筛选条件
     * @return 是否成功
     */
    @Override
    public ServerResult deleteById(AdminMenuQueryVO adminMenuQueryVO) {
        List<Long> ids = adminMenuQueryVO.getIds();
        if (CollUtil.isEmpty(ids)) {
            return ServerResult.fail("ids 参数不能为空");
        }
        List<AdminMenuDO> adminMenuList = listByIds(ids);
        if (CollUtil.isEmpty(adminMenuList)) {
            return ServerResult.success();
        }
        //todo 校验是否可以逻辑删除
        LambdaUpdateWrapper<AdminMenuDO> updateWrapper = new LambdaUpdateWrapper<AdminMenuDO>()
                .in(AdminMenuDO::getId, ids)
                .set(AdminMenuDO::getEnableFlag, Constant.UNABLE_TWENTY);
        update(updateWrapper);
        return ServerResult.success();
    }
}
